#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    BaseRestEditor(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    normalMode=true;
    entry_=T_POSITION;
    baseUrl_=URL_POSITION;
    topics=QStringList();

    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);

    tableModel=new TableModel(this);
    ui->tableView->setModel(tableModel);

    createMenu();
    updateListView();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::generateMenu()
{
    menu->addAction(QStringLiteral("添加... (F1)"),this,&Widget::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("编辑... (F2)"),this,&Widget::editItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新列表 (F4)"),this,&Widget::updateListView);
    menu->addAction(QStringLiteral("更新视图 (F5)"),this,&Widget::updateTableView);
    menu->addSeparator();
    QMenu *optMenu=menu->addMenu("选项");
    optMenu->addAction(QStringLiteral("删除 (Delete)"),this,&Widget::removeItem);
}

void Widget::updateListView()
{
    QString url=baseUrl_ + "get_topics";
    emit get(url);
}

void Widget::updateTableView()
{
    if(selectTopic.isEmpty())
        return;

    normalMode=true;
    tableModel->clear();
    QString url = baseUrl_ + "by_topic/" + selectTopic;
    emit get(url);
}

void Widget::updateSearchView()
{
    if(keyword.isEmpty())
        return;

    normalMode=false;
    tableModel->clear();
    QString url = baseUrl_ + "search/" + keyword;
    emit get(url);
}

void Widget::newItem()
{
    P_AddItem addItem(this,topics);
    if(!selectTopic.isEmpty())
        addItem.setTopic(selectTopic);

    connect(&addItem,&P_AddItem::addItem,
            this,&Widget::onAddItem);
    if(addItem.exec() == QDialog::Accepted)
        delayUpdateView();
}

void Widget::editItem()
{
    if(selectTopic.isEmpty())
        return;

    QModelIndex index=ui->tableView->currentIndex();
    if(!index.isValid())
        return;

    int id=sp_fetchId(index);
    QString label=sp_fetchString(index,2);
    QString detail=sp_fetchString(index,3);
    QString addition=sp_fetchString(index,4);

    P_AddItem addItem(this,topics);
    addItem.setItem(id,selectTopic,label,detail,addition);

    connect(&addItem,&P_AddItem::updateItem,this,&Widget::onUpdateItem);
    if(addItem.exec() == QDialog::Accepted)
        delayUpdateView();
}

void Widget::removeItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(!index.isValid())
        return;

    QMessageBox::StandardButton btn=MESSAGE_DELETE_CONFIRM
            if(btn == QMessageBox::Ok)
    {
            int id=sp_fetchId(index);
            if(id == 0)
    {
            MESSAGE_EXCEPTIOM
}
            else {
            QString url=baseUrl_ + "remove/" + QString::number(id);
            emit get(url);
            delayUpdateView();
}
}
}

void Widget::onAddItem(const QJsonObject &obj)
{
    QString url=baseUrl_ + "insert";
    emit post(url,QJsonDocument(obj).toJson(QJsonDocument::Compact));
}

void Widget::onUpdateItem(const QJsonObject &obj)
{
    QString url=baseUrl_ + "update";
    emit post(url,QJsonDocument(obj).toJson(QJsonDocument::Compact));
}

void Widget::delayUpdateView()
{
    QEventLoop loop;
    QTimer::singleShot(200,&loop,&QEventLoop::quit);
    loop.exec();

    if(normalMode)
        updateTableView();
    else
        updateSearchView();
}

void Widget::onReceivedObject(const QJsonObject &object)
{
    if(!object.contains("entry") || object.value("entry").toString() != entry_)
        return;

    if(object.contains("detail"))
    {
        QJsonArray array=object.value("detail").toArray();
        if(array.isEmpty())
            return;

        tableModel->setArray(array,positionColumns,positionColumnHeaders);
        ui->tableView->hideColumn(0);
        ui->tableView->setColumnWidth(1,120);
        ui->tableView->setColumnWidth(2,180);
    }
    else if(object.contains("topics"))
    {
        QJsonArray array=object.value("topics").toArray();
        Piv piv(array);
        topics=piv.getStringList();
        listModel->setStringList(topics);
    }
}

void Widget::on_kw_returnPressed()
{
    keyword=ui->kw->text().trimmed();
    updateSearchView();
}

void Widget::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectTopic=index.data().toString();
        updateTableView();
    }
}
